package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.common.util.RedisUtil;
import com.sg.dto.base.component.req.ImplementHoverConcurrentBatchReq;
import com.sg.entity.OmsConcurrentTransactionBatches;
import com.sg.service.base.component.MOmsConcurrentTransactionBatchesService;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsConcurrentTransactionBatchesMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsConcurrentTransactionBatchesServiceImpl
        extends ServiceImpl<OmsConcurrentTransactionBatchesMapper, OmsConcurrentTransactionBatches>
        implements MOmsConcurrentTransactionBatchesService {

    @Resource
    RedisUtil redisUtil;

    @Autowired
    OmsConcurrentTransactionBatchesMapper omsConcurrentTransactionBatchesMapper;

    /**
     * 2-5-10新增并发事务批次[8330]
     * gen by moon at 1/23/2024, 11:31:56 PM
     */
    @Trace(operationName = "2-5-10新增并发事务批次")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addConcurrentBatch(OmsConcurrentTransactionBatches omsConcurrentTransactionBatches) {
        omsConcurrentTransactionBatches.setConcurrentTransactionBatchId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsConcurrentTransactionBatches.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME

        if (super.save(omsConcurrentTransactionBatches)) {
            return omsConcurrentTransactionBatches.getConcurrentTransactionBatchId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-5-10删除并发事务批次[8331]
     * gen by moon at 1/23/2024, 11:31:56 PM
     */
    @Trace(operationName = "2-5-10删除并发事务批次")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteConcurrentBatch(String concurrentTransactionBatchId) {

        return super.removeById(concurrentTransactionBatchId);
    }

    /**
     * 2-5-10执行悬停并发事务批次[8332]
     * gen by moon at 1/23/2024, 11:31:59 PM
     */
    @Trace(operationName = "2-5-10执行悬停并发事务批次")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public List<OmsConcurrentTransactionBatches> implementHoverConcurrentBatch(ImplementHoverConcurrentBatchReq reqDto) {
        try {
            long count = 60000;
            while (true) {
                if (count == 0) {
                    break;
                }
                QueryWrapper<OmsConcurrentTransactionBatches> queryWrapper = new QueryWrapper<>();
                Map<String, Object> fieldInfoMap = null;
                fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
                CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
                queryWrapper.orderByDesc("create_time");
                if (super.list(queryWrapper) != null && super.list(queryWrapper).size() > 0) {
                    List<OmsConcurrentTransactionBatches> retData = new ArrayList<>();
                    retData.add(super.list(queryWrapper).get(0));
                    return retData;
                }
                count--;
                Thread.sleep(100);
            }
            redisUtil.set("2-5-10执行悬停并发事务批次-未找到数据-并返回空对象", reqDto.getConcurrentTransactionBatchId());
            return new ArrayList<>();
//         throw new BizException("-1",false);
        } catch (Exception e) {
            e.printStackTrace();
            redisUtil.set("2-5-10执行悬停并发事务批次-数据库本身异常-并返回空对象", reqDto.getConcurrentTransactionBatchId());
            return new ArrayList<>();
//         throw new BizException("-1",e,false);
        }

    }

    /**
     * 2-5-10新增异常消息数量处理批次[8336]
     * gen by moon at 1/25/2024, 2:35:18 AM
     */
    @Trace(operationName = "2-5-10新增异常消息数量处理批次")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addAbnormalMsgLocConcurrentBatch(OmsConcurrentTransactionBatches omsConcurrentTransactionBatches) {
        omsConcurrentTransactionBatches.setConcurrentTransactionBatchId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsConcurrentTransactionBatches.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (super.save(omsConcurrentTransactionBatches)) {
            return omsConcurrentTransactionBatches.getConcurrentTransactionBatchId();
        } else {
            return "-1";
        }

    }
}
